package com.yaodian100.ec.generate.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.FileUtils;

import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.ec.generate.entity.GenerateAdvertise;
import com.yaodian100.ec.generate.entity.IdxWeeklyRecommendAdvItem;
import com.yaodian100.ec.generate.entity.ViewPromotionAdvertise;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.shelf.entity.Shelf;

public class IdxWeeklyRecommendAdvGenerater extends AbstractAdvGenerater {

	public static final String ADV_TYPE_IDX_WEEKLYRECOMMEND = "IDX_WEEKLYRECOMMEND";

	private final GenerateServiceImpl generateServiceImpl;

	IdxWeeklyRecommendAdvGenerater(GenerateServiceImpl impl) {
		this.generateServiceImpl = impl;
	}

	boolean generateIdxWeeklyRecommendAdv() throws Exception {
		String advType = ADV_TYPE_IDX_WEEKLYRECOMMEND;

		List<Long> activeAdvIdList = new ArrayList<Long>();
		
		CommonCriteria criteria = new CommonCriteria();
		criteria.addEq("advType", advType);
		
		List<ViewPromotionAdvertise> viewPromotionAdvs = this.generateServiceImpl.getViewPromotionAdvertiseDao()
				.getListPageable(criteria, new String[] { "showSeq asc", "startDate", "advTarget" }, 0, -1);
		if (viewPromotionAdvs != null) {
			String cateId = "IDX";
			boolean mkDirsuccess = this.generateServiceImpl.mkGenerateHtmlDir(advType, cateId);
			if (!mkDirsuccess) {
				throw new RuntimeException("建立 html 目錄失敗");
			}
			
			this.generateServiceImpl.deleteFiles(ServerValue.getFrontContentHtmlPath() + cateId, advType);
			
			for (ViewPromotionAdvertise viewPromotionAdv : viewPromotionAdvs) {
				int index = viewPromotionAdvs.indexOf(viewPromotionAdv);
				if (index > 2) {
					break;
				}

				Reader reader = null;
				Properties prop = null;
				
				prop = new Properties();
				reader = new StringReader(viewPromotionAdv.getContents());
				prop.load(reader);
				reader.close();
				
				prop.put("advType", advType);
				prop.put("contextPath", ServerValue.getFrontContextPath());
				
				List<IdxWeeklyRecommendAdvItem> recommendProductList = new ArrayList<IdxWeeklyRecommendAdvItem>();
				addOneRecommnedRecordToListByProperty(prop, "recommendProductId1", "recommendAdCode1", recommendProductList);
				addOneRecommnedRecordToListByProperty(prop, "recommendProductId2", "recommendAdCode2", recommendProductList);
				addOneRecommnedRecordToListByProperty(prop, "recommendProductId3", "recommendAdCode3", recommendProductList);
				prop.put("recommendProductList", recommendProductList);
				
				List<IdxWeeklyRecommendAdvItem> freshProductList = new ArrayList<IdxWeeklyRecommendAdvItem>();
				addOneRecommnedRecordToListByProperty(prop, "freshProductId1", "freshAdCode1", freshProductList);
				addOneRecommnedRecordToListByProperty(prop, "freshProductId2", "freshAdCode2", freshProductList);
				addOneRecommnedRecordToListByProperty(prop, "freshProductId3", "freshAdCode3", freshProductList);
				prop.put("freshProductList", freshProductList);
				
				List<IdxWeeklyRecommendAdvItem> topicalProductList = new ArrayList<IdxWeeklyRecommendAdvItem>();
				addOneRecommnedRecordToListByProperty(prop, "topicalProductId1", "topicalAdCode1", topicalProductList);
				addOneRecommnedRecordToListByProperty(prop, "topicalProductId2", "topicalAdCode2", topicalProductList);
				addOneRecommnedRecordToListByProperty(prop, "topicalProductId3", "topicalAdCode3", topicalProductList);
				prop.put("topicalProductList", topicalProductList);
				
				String htmlFilePath = defaultAdvHtmlFileFullPath(advType, cateId, index);
				String content = this.generateServiceImpl.getTemplateService().format("static_IdxWeeklyRecommend", prop);
				FileUtils.writeStringToFile(new File(htmlFilePath.toString()), content.substring(content.indexOf('<'), content
						.length()));
				
				activeAdvIdList.add(Long.valueOf(viewPromotionAdv.getId()));
			}
		}
		
		this.generateServiceImpl.updateAdvStatus(advType, activeAdvIdList);
		return true;
	}

	private void addOneRecommnedRecordToListByProperty(Properties prop, String productIdPropertyName,
		String adCodePropertyName, List<IdxWeeklyRecommendAdvItem> viewFlagProductList) {
		String productId = prop.getProperty(productIdPropertyName);
		String adCode = prop.getProperty(adCodePropertyName);

		IdxWeeklyRecommendAdvItem result;
		result = new IdxWeeklyRecommendAdvItem();

		Product recommendProduct = this.generateServiceImpl.getProductService().get(productId);
		if (recommendProduct == null) {
			return;
		}
		
		Shelf recommendProductShelf1 = this.generateServiceImpl.getShelfService().getActiveShelfByProductId(productId);
		if (recommendProductShelf1 == null) {
			result.setPromotionName(null);
			result.setShelfPrice(null);
		} else {
			result.setPromotionName(recommendProductShelf1.getPromoteName());
			result.setShelfPrice(recommendProductShelf1.getShelfPrice());
		}

		result.setProductId(productId);
		result.setProductName(recommendProduct.getProductName());
		result.setAdCode(adCode);

		viewFlagProductList.add(result);
	}

}
